package hhm.android.main

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.FragmentEvent
import hhm.android.base.fragment.NewLazyFragment
import hhm.android.library.bus.Bus

import hhm.android.library.utils.BackListener
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.*
import siau.android.http.model.ChildModel
import siau.android.library.KeyString

class OneNoDataFragment(var statusBarHeightPx: Int) : NewLazyFragment() {

    constructor() : this(20)

    lateinit var buy: TextView
    lateinit var topView: View
    lateinit var add: TextView
    lateinit var recycler: RecyclerView
    lateinit var change: ImageView
    private lateinit var list: ArrayList<ChildModel>
    private var childIdNow = -1
    private var childAdapter: ChildAdapter? = null

    override fun getContentViewId(): Int {
        return R.layout.fragment_one_no_data
    }

    override fun initView(view: View?) {
        super.initView(view)
        buy = view?.findViewById(R.id.fragment_one_no_data_buy)!!
        change = view.findViewById(R.id.fragment_one_no_data_change)!!
        add = view.findViewById(R.id.fragment_one_no_data_viewpager_inner_start_add)!!
        recycler = view.findViewById(R.id.fragment_one_no_data_viewpager_inner_recycler)!!

        topView = view.findViewById(R.id.fragment_one_no_data_top_view)
        val layoutParams = topView.layoutParams
        layoutParams.height = statusBarHeightPx
        topView.layoutParams = layoutParams

        initChildren(true)
    }

    override fun getData() {
        super.getData()

        HttpHelper().myBabyList()?.subscribe({

            list.clear()
            list.addAll(it)
            SBApplication.userData.children = it
            var has = false
            list.forEach { i ->
                if (i.childId == childIdNow && i.childId != -1) {
                    i.select = true
                    SBApplication.communityChildId = i.childId
                    has = true
                }
            }
            if (!has && list.size > 0) {
                list[0].select = true
                childIdNow = list[0].childId
                SBApplication.communityChildId = childIdNow
            }

            if (SBApplication.userData.isCreator == 1) {
                val c = ChildModel(-1, "", "", false, -1, 0, 0, -1.0, -1.0)
                list.add(c)
            }

            childAdapter?.setData(list)

        }, {
            removeLoadingFragment()
            showToast(it.message)
        })

        if (SBApplication.userData.isCreator != 1 && list.size > 0 && list[list.size - 1].childId == -1) {
            list.removeAt(list.size - 1)
            childAdapter?.notifyDataSetChanged()
        }
    }

    private fun initChildren(really: Boolean) {
        list = ArrayList()

        val lm = LinearLayoutManager(activity)
        lm.orientation = LinearLayoutManager.HORIZONTAL
        recycler.layoutManager = lm

        childAdapter = activity?.let {
            ChildAdapter(it, list, { i ->
                childIdNow = list[i].childId
                SBApplication.communityChildId = childIdNow
                childAdapter?.notifyDataSetChanged()
                getData()
            }, object : BackListener {
                override fun callBack() {
                    ARouter.getInstance().build(BABY_ROUTE).withInt(KeyString.type, 0).navigation()
                }
            })
        }!!
        recycler.adapter = childAdapter

        getData()
    }

    override fun initEvent() {
        super.initEvent()

        buy.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SystemUtil.isPkgInstalled(activity, "com.taobao.taobao")) {
                    SystemUtil.gotoShop(activity, "taobao://shop504445296.m.taobao.com")
                } else {
                    showToast("您还没有安装淘宝客户端！")
                }
            }
        })
        add.setOnClickListener {
            if (SBApplication.checkHasChildren()) {
                ARouter.getInstance().build(MACHINE_ROUTER).navigation()
            } else {
                activity?.let { it1 ->
                    getAddChildDialog(it1) { ARouter.getInstance().build(BABY_ROUTE).withInt(KeyString.type, 0).navigation() }
                }
            }
        }
        change.setOnClickListener {
            if (!SBApplication.startShowChangeFamilySheet) {
                SBApplication.startShowChangeFamilySheet = true

                HttpHelper().getFamilyList()?.compose(RxLifecycle.bindUntilEvent(lifecycle(), FragmentEvent.STOP))?.subscribe({
                    //判断有没有自己创建家庭
                    if (it.hasOwnFamily == 0) {
                        activity?.let { it1 ->
                            getSureDialog(it1, object : OnMultiClickListener() {
                                override fun onMultiClick(v: View?) {
//                                    if (SBApplication.startCalculate.value == true) {
//                                        SBApplication.startCalculate.value = false
//                                    }
                                    Bus.getDefault().post(KeyString.createSelfFamily)
                                    SBApplication.startShowChangeFamilySheet = false
                                }
                            }, cancelListener = object : OnMultiClickListener() {
                                override fun onMultiClick(v: View?) {
                                    val bottomSingleFamilySelectSheet = activity?.let { it1 ->
                                        BottomSingleFamilySelectSheet(it1, it) { it2 ->
//                                            if (SBApplication.startCalculate.value == true) {
//                                                SBApplication.startCalculate.value = false
//                                            }
                                            val m = activity as MainActivity
                                            m.changeFamily(it2)
                                        }
                                    }
                                    bottomSingleFamilySelectSheet?.show()
                                }
                            }, content = getString(R.string.sure_to_create_self_family), showTitle = false)
                        }
                    } else {
                        val bottomSingleFamilySelectSheet = activity?.let { it1 ->
                            BottomSingleFamilySelectSheet(it1, it) { it2 ->
                                val m = activity as MainActivity
                                m.changeFamily(it2)
                            }
                        }
                        bottomSingleFamilySelectSheet?.show()
                    }
                }, {
                    showToast(it.message)
                    SBApplication.startShowChangeFamilySheet = false
                })
            }
        }
    }

//    fun refreshChildren() {
////        initChildren(false)
//        getData()
//    }
}